package cn.itcast.common.utils;

import com.google.gson.Gson;
import com.qiniu.common.QiniuException;
import com.qiniu.common.Zone;
import com.qiniu.http.Response;
import com.qiniu.storage.BucketManager;
import com.qiniu.storage.Configuration;
import com.qiniu.storage.UploadManager;
import com.qiniu.storage.model.DefaultPutRet;
import com.qiniu.util.Auth;


/**
 * @author: inexorable
 * @date: 2019/7/17 21:34
 * @VERSION V1.0
 * @description 七牛云存储文件或图片
 */

public class QiniuUtils {
    /**
     * accessKey:授权码
     * secretKey：密钥
     * bucket：名称空间
     */
    private static  String accessKey = "vh0-O6Yxr2t-im8G8GbMkSziawt3ySUbOhsFNhDZ";
    private static  String secretKey = "An20euCqQHXdGzRTAIDZSaoPuJ6Qv06pIDrFF5Z8";
    private static  String bucket = "yangtaopicture";

    /**
     * 文件或图片上传
     * @param bytes
     * @param fileName
     */
    public static void upload2Qiniu(byte[] bytes, String fileName) {
        //配置文件
        Configuration cfg = new Configuration(Zone.zone0());
        //上传管理者
        UploadManager uploadManager = new UploadManager(cfg);
        //上传者
        Auth auth = Auth.create(accessKey, secretKey);
        //上传空间
        String uploadToken = auth.uploadToken(bucket);
        try {
            //文件上传
            Response response = uploadManager.put(bytes, fileName, uploadToken);
            //解析上传结果
            DefaultPutRet putRet = new Gson().fromJson(response.bodyString(), DefaultPutRet.class);
            System.out.println(putRet.hash);
            System.out.println(putRet.key);
        } catch (QiniuException e) {
            Response r = e.response;
            System.err.println(r.toString());
            try {
                System.err.println(r.bodyString());
            } catch (QiniuException ex) {
                ex.printStackTrace();
            }
        }
    }


    /**
     * 文件或图片的地址上传
     * @param filePath
     * @param fileName
     */
    public static void upload2Qiniu(String filePath, String fileName) {
        //配置文件
        Configuration cfg = new Configuration(Zone.zone0());
        //定义上传管理者
        UploadManager uploadManager = new UploadManager(cfg);
        //定义上传的人
        Auth auth = Auth.create(accessKey, secretKey);
        //定义上传的地址
        String uploadToken = auth.uploadToken(bucket);
        try {
            //上传文件或图片地址
            Response response = uploadManager.put(filePath, fileName, uploadToken);
            //解析上传结果
            DefaultPutRet putRet = new Gson().fromJson(response.bodyString(), DefaultPutRet.class);
            System.out.println(putRet.key);
            System.out.println(putRet.hash);
        } catch (QiniuException e) {
            Response r = e.response;
            System.err.println(r.toString());
            try {
                System.err.println(r.bodyString());
            } catch (QiniuException ex) {
                ex.printStackTrace();
            }
        }
    }

    /**
     * 删除七牛云存储空间中的指定文件或图片
     * @param fileName
     */
    public static void deleteQiniuImageOrFile(String fileName) {
        //创建配置文件
        Configuration cfg = new Configuration(Zone.zone0());
        //创建删除者
        Auth auth = Auth.create(accessKey, secretKey);
        //创建存储空间管理者
        BucketManager bucketManager = new BucketManager(auth, cfg);
        try {
            //删除指定图片或文件
            bucketManager.delete(bucket,fileName);
        } catch (QiniuException e) {
            System.err.println(e.response.toString());
            System.err.println(e.code());
        }

    }


}
